import datetime

import pandas as pd
from icalendar import Calendar, Event
import time

# 创建Calendar对象
cal = Calendar()
cal.add('prodid', '-//Example Inc.//Example Calendar//EN')
cal.add('version', '2.0')
cal.add('calscale', 'gregorian')

# 创建接下来30天的事件
dates=pd.read_excel("D:\excel\班表.xlsx",0)
#print(dates)
usrs=dates["日期"].unique()
now = datetime.datetime.utcnow()
for day in usrs:
    start_time = time.strptime(dates[dates["日期"]==day]["上班时间"].unique()[0],"%Y-%m-%d %H:%M:%S")
    sbsj=datetime.datetime.fromtimestamp(time.mktime(start_time))
    print(sbsj)
    end_time = time.strptime(dates[dates["日期"]==day]["下班时间"].unique()[0],"%Y-%m-%d %H:%M:%S")#start_time + datetime.timedelta(hours=1)
    xbsj=datetime.datetime.fromtimestamp(time.mktime(end_time))
    print(xbsj )
    event = Event()
    event.add('summary', '上班')
    event.add('dtstart', sbsj)
    event.add('dtend', xbsj)
    event.add('description', 'This is an event for the day.')
    cal.add_component(event)

# 写入.ics文件
with open('calendar.ics', 'wb') as f:
    f.write(cal.to_ical())